package com.xin.ssm;

import com.xin.ssm.entity.Employee;
import com.xin.ssm.mapper.DepartmentMapper;
import com.xin.ssm.mapper.EmployeeMapper;
import org.apache.ibatis.session.SqlSession;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.UUID;

/**
*测试dao层的工作
 * 推荐Spring的项目就可以使用Spring的单元测试，可以自动注入我们需要的组件
 * 1.需要在pom.xml中导入spring test单元测试模块
 * 2.@ContextConfiguration指定spring配置文件的位置
 * 3.@RunWith中的value值用来指定选用哪个单元测试来运行，这里指定用Spring的单元测试
* @return
*@author 橘子星星
*@creed:good good study,day day happy
*@date 2022/2/17 9:08
*/
@RunWith(value = SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-config.xml"})
public class MapperTest1 {
    @Autowired
    DepartmentMapper departmentMapper;
    @Autowired
    EmployeeMapper employeeMapper;
    @Autowired
    SqlSession sqlSession;

    @org.junit.Test
    public void test1(){

//        ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-config.xml");
//        DepartmentMapper bean = ioc.getBean(DepartmentMapper.class);
        //测试能否创建dao层中的类
        System.out.println(departmentMapper);
        System.out.println(employeeMapper);
        //批量插入
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
        for (int i = 0; i < 20; i++) {
            String uid = UUID.randomUUID().toString().substring(0, 5) + i;
            mapper.insertSelective(new Employee(null,uid,"M",uid + "@atguigu.com",2,null));

        }
    }

}
